﻿
Imports System.Data.Common
Imports DbDataLib

Namespace Reporting

    Public Class Report

        Public Sub New()
        End Sub

        Public Property Id As Integer
        Public Property Name As String
        Public Property Statement As String
        Public Property ConnectionName As String
        Public Property IdColumn As String
        Public Property StartType As String

        Public Function GetData() As DataTable

            Dim dataService As DbDataService = _
                DbDataRepository.GetInstance().GetDbDataService(ConnectionName, True)

            Dim cmd As DbCommand = dataService.Factory.CreateCommand()

            cmd.CommandText = Statement

            Return dataService.GetDataTable(cmd)

        End Function

        Public Function GetStartType() As Type

            Dim type As System.Type = Nothing

            type = System.Type.GetType(StartType)

            If type Is Nothing Then

                For Each a As System.Reflection.Assembly In AppDomain.CurrentDomain.GetAssemblies()

                    type = a.GetType(StartType)

                    If type IsNot Nothing Then Exit For

                Next

                If type Is Nothing Then

                    Throw New TypeLoadException()

                End If

            End If

            Return type

        End Function

        Public Sub Validate()

            If Name Is Nothing OrElse Name = String.Empty OrElse _
                Statement Is Nothing OrElse Statement = String.Empty OrElse _
                ConnectionName Is Nothing OrElse ConnectionName = String.Empty OrElse _
                IdColumn Is Nothing OrElse IdColumn = String.Empty OrElse _
                StartType Is Nothing OrElse StartType = String.Empty Then
                Throw New NoNullAllowedException("Bitte füllen Sie alle Werte Fehlerfrei und gültig aus.")
            End If

        End Sub


    End Class

End Namespace
